const { appNodeModules, appPackageJson } = require('./paths');
const packageContent = require(appPackageJson);


module.exports = {
  alias: {},
  define: {},
  devPublicPath: '/',
  filename: `[name]${getVersion()}.js`,
  // resolve.extensions
  extensions: ['.js', '.jsx', '.json', '.html', '.ts', '.tsx'],
  // resolve.modules
  modules: ['node_modules', appNodeModules],
  devServer: {
    disableHostCheck: true,
    compress: true,
    clientLogLevel: 'none',
    hot: true,
    publicPath: '/',
    quiet: true,
    watchOptions: {
      ignored: /node_modules/,
      aggregateTimeout: 600,
    },
    before(app) {
      // todo add user's before
      // user.before(app);
      app.use((req, res, next) => {
        // set cros for all served files
        res.set('Access-Control-Allow-Origin', '*');
        next();
      });
    },
  },
  entry: 'src/index.js',
  externals: {},
  hash: false,
  injectBabel: 'polyfill',
  minify: true,
  outputAssetsPath: {
    js: 'js',
    css: 'css',
  },
  outputDir: 'build',
  proxy: {},
  publicPath: '/',
  targets: 'last 2 versions, Firefox ESR, > 1%, ie >= 9, iOS >= 8, Android >= 4',
  vendor: true,
};

function getVersion(){
  let verNo = '';
  const fv = packageContent['file-version'];

  if(fv==='version'){
    verNo = `_v${packageContent.version}`;
  }else if(fv==='date'){
    const now = new Date();
    const fill0 = v => `0${v}`.slice(-2);
    verNo = `_d${now.getFullYear()+fill0(now.getMonth()+1)+fill0(now.getDate())}`;
  }else if(fv==='time'){
    verNo = `_t${Date.now()}`;
  }
  return verNo;
}